<?php
require_once("config/config.php");

//
// extract_structureDiplome
//
function extract_structureDiplome($structureDiplome,$idObjetAPOGEE=0,$level=0) {
 $diplome=array();
 if (isset($structureDiplome[$idObjetAPOGEE])) {
  foreach ($structureDiplome[$idObjetAPOGEE] as $element) {
   $subdiplome=extract_structureDiplome($structureDiplome,$element[idObjetAPOGEE],$level+1);
   $id=uniqid();
   $newElement[$id]=array (
     idObjetAPOGEE => $element[idObjetAPOGEE],
     codeAPOGEE => $element[codeAPOGEE],
     version    => $element[version],
     intitule   => $element[intitule],
     niveau     => $level
    );
   $diplome=array_merge($diplome,$subdiplome,$newElement);
  }
 }
 return $diplome;
}

//
// extract_results
//
function extract_results($results,$structureDiplome,$year,$idObjetAPOGEE=0,$level=0) {
 $resultsVDI=array();
 if (isset($structureDiplome[$idObjetAPOGEE])) {
  foreach ($structureDiplome[$idObjetAPOGEE] as $element) {
   $subresults=extract_results($results,$structureDiplome,$year,$element[idObjetAPOGEE],$level+1);
   $resultFound=FALSE;
   $newResultsVDI=NULL;
   foreach ($results as $result) {
    if ( ($result[annee] == $year) and ($result[idObjetAPOGEE] == $element[idObjetAPOGEE]) ) {
     $resultFound = TRUE;
     $newResultsVDI[$result[idResultat]]=$result;
     $newResultsVDI[$result[idResultat]][niveau]=$level;
     //break;
    }
   }
   if(!$resultFound) {
    $idResultat=uniqid();
    $result=array (
     idResultat => $idResultat,
     idObjetAPOGEE => $element[idObjetAPOGEE],
     codeAPOGEE => $element[codeAPOGEE],
     intitule   => $element[intitule],
     niveau     => $level
    );
    $newResultsVDI[$result[idResultat]]=$result;
   }
   $resultsVDI=array_merge($resultsVDI,$subresults,$newResultsVDI);
  }
 }
 return $resultsVDI;
}

//
// print_results
//
function print_results($results,$noEtudiant,$year,$count=0,$details=FALSE,$historique=FALSE,$avecControles=FALSE) {
 global $CFG;
 $bgcolors = $CFG->bgcolors;
 $oldcolor = $CFG->oldcolor;
  
?>
<table border="1">
 <tr>
  <th>Code APOGEE</th>
  <th>Intitul&eacute;</th>
  <th>Note</th>
  <th>R&eacute;sultat</th>
  <th>Session</th>
<?php
 if ($details) {
?>
  <th>Date saisie</th>
  <th>Source saisie</th>
<?php
 }
?>
 </tr>
<?php
 $i=0;
 $oldIdObjetAPOGEE="";
 foreach ($results as $result) {
  list($date,$heure)=explode(" ",$result['date']);
  list($annee,$mois,$jour)=explode("-",$date);
  $affichage=TRUE;
  if ($oldIdObjetAPOGEE != $result['idObjetAPOGEE']) {
   if (($oldIdObjetAPOGEE != "")&&($avecControles)) {
    print_student_marks($noEtudiant,$oldIdObjetAPOGEE,$details,$year,$historique);
   }
   $oldIdObjetAPOGEE = $result['idObjetAPOGEE'];
   $bgcolor=$bgcolors[$result['niveau']];
  } else {
   $bgcolor = $oldcolor;
   $affichage = $historique;
  }
  if ($affichage) {
   echo "<tr bgcolor=\"$bgcolor\">\n";
   echo "<td>";
   for ($j=0;$j<$result['niveau'];$j++) echo "&nbsp; ";
   echo htmlentities($result['codeAPOGEE']);
   echo "</td>\n";
   echo "<td>".htmlentities($result['intitule'])."</td>\n";
   echo "<td>";
   if ($result['valeur'] != "") {
    if ($result['masquer']) {
     echo "( {$result['valeur']} )";
    } else {
     echo $result['valeur'];
    }
   } else if (!has_saisieAutorisee($noEtudiant,$result['idObjetAPOGEE'],$year)) {
    echo "non autoris&eacute;";
   }
   echo "</td>\n";

   echo "<td>";
   if ($result['resultat'] !== "") {
    if ($result[masquer]) {
     echo "( {$result['resultat']} )";
    } else {
     echo $result['resultat'];
    }
   }
   echo "</td>\n";
   
   echo "<td>";
   if (isset($result['session'])) {
    if ($result['session']==0) {
     echo "unique";
    } else {
     echo $result['session'];
    }
   }
   echo "</td>\n";

   if ($details) {
?>
  <td><?php if ($jour <> "") echo $jour."/".$mois."/".$annee." ".$heure; ?></td>
  <td><?php echo htmlentities($result['source']); ?></td>
<?php
   }
?>
 </tr>
<?php
   $i++;
   if ($i == $count) { break; }
  }
 }
 if ($avecControles) {
  print_student_marks($noEtudiant,$oldIdObjetAPOGEE,$details,$year,$historique);
 }
?>
</table>
<?php
}

//
// print_student_marks
//
function print_student_marks($noEtudiant,$codeAPOGEE,$details,$year,$historique=FALSE) {
 global $CFG;
 
 if ($marks=get_student_marks($noEtudiant,$codeAPOGEE,$year)) {
  $marks=group_marks_by_idControle($marks);
  foreach ($marks as $mark) {
   if (!$historique) {
    $affichage=FALSE;
    if ($mark['idControle'] != $currentIdControle) {
     $affichage=($mark['valeur'] !== "");
     $currentIdControle=$mark['idControle'];
    }
   } else {
    $affichage=TRUE;
   }
   if ($affichage) {
    echo "<tr bgcolor=\"{$CFG->markcolor}\">\n";
    echo "<td></td>";
    echo "<td>".htmlentities($mark['intituleControle'])." (coeff {$mark['coefficient']})</td>\n";
    echo "<td>{$mark['valeur']}</td>\n";
    echo "<td></td><td></td>\n";
    if ($details) {
     list($date,$heure)=explode(" ",$mark['date']);
     list($annee,$mois,$jour)=explode("-",$date);
?>
<td><?php if ($jour <> "") echo $jour."/".$mois."/".$annee." ".$heure; ?></td>
<td><?php echo htmlentities($mark['source']); ?></td>
<?php
    }
?>
</tr>
<?php
   }
  }
 }
}


//
// print_marks
//
function print_marks($results,$details=FALSE,$historique=FALSE) {
 global $CFG;
 $bgcolor = $CFG->markcolor;
  
?>
<table border="1">
 <tr>
  <th>Code APOGEE</th>
  <th>Intitul&eacute;</th>
  <th>Contr&ocirc;le</th>
  <th>Coefficient</th>
  <th>Note</th>
<?php
 if ($details) {
?>
  <th>Date saisie</th>
  <th>Source saisie</th>
<?php
 }
?>
 </tr>
<?php
 $i=0;
 $currentIdControle=0;
 foreach ($results as $result) {
  if (!$historique) {
   $affichage=FALSE;
   if ($result['idControle'] != $currentIdControle) {
    $affichage=($result['valeur'] !== "");
    $currentIdControle=$result['idControle'];
   }
  } else {
   $affichage=TRUE;
  }
  if ($affichage) {
   list($date,$heure)=explode(" ",$result['date']);
   list($annee,$mois,$jour)=explode("-",$date);
   echo "<tr bgcolor=\"$bgcolor\">\n";
   echo "<td>";
   echo htmlentities($result['codeAPOGEE']);
   echo "</td>\n"; 
   echo "<td>".htmlentities($result['intituleAPOGEE'])."</td>\n";
   echo "<td>".htmlentities($result['intituleControle'])."</td>\n";
   echo "<td>{$result['coefficient']}</td>\n";
   echo "<td>";
   echo $result['valeur'];
   echo "</td>\n";

   if ($details) {
?>
  <td><?php if ($jour <> "") echo $jour."/".$mois."/".$annee." ".$heure; ?></td>
  <td><?php echo htmlentities($result['source']); ?></td>
<?php
  }
?>
 </tr>
<?php
  }
 }
?>
</table>
<?php
}

//
// group_results_by_idObjetAPOGEE
//
function group_results_by_idObjetAPOGEE($results) {
 $tempResults=array();
 $newResults = array();
 foreach ($results as $idResult => $result) {
  $tempResults[$result['idObjetAPOGEE']][$idResult]=$result;
 }
 foreach ($tempResults as $idObjetAPOGEE => $results) {
  foreach ($results as $idResult => $result) {
   $newResults[$idResult]=$result;
  }
 }
 return $newResults;
}

//
// group_marks_by_idControle
//
function group_marks_by_idControle($marks) {
 $tempMarks=array();
 $newMarks = array();
 foreach ($marks as $idMark => $mark) {
  $tempMarks[$mark['idControle']][$idMark]=$mark;
 }
 foreach ($tempMarks as $idControle => $marks) {
  foreach ($marks as $idMark => $mark) {
   $newMarks[$idMark]=$mark;
  }
 }
 return $newMarks;
}
?>